package cn.acmoj.oj.service;

import cn.acmoj.oj.bean.Solution;
import cn.acmoj.oj.bean.SolutionPro;
import cn.acmoj.oj.bean.SourceCode;
import cn.acmoj.oj.bean.SourceCodeUser;
import cn.acmoj.oj.dao.ISolutionDao;
import cn.acmoj.oj.dao.ISourceCodeDao;
import cn.acmoj.oj.dao.ISourceCodeUserDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * cn.acmoj.oj.service
 * Springer
 * 2017-12-24
 */
@Service
public class SolutionService {

    @Resource
    private ISolutionDao iSolutionDao;
    @Resource
    private ISourceCodeDao iSourceCodeDao;
    @Resource
    private ISourceCodeUserDao iSourceCodeUserDao;

    public List<SolutionPro> getAll(Integer contest_id, Integer page, Integer num, Integer pid, String user_id, Integer lang, Integer result) {
        Map<String, Object> param = new HashMap<>();
        param.put("start", (page - 1) * num);
        param.put("count", num);
        if (contest_id != -1) {
            param.put("contest_id", contest_id);
            System.out.println(contest_id);
        }
        System.out.println(contest_id);
        if (pid != -1) {
            param.put("pid", pid);
        }
        if (!user_id.isEmpty()) {
            param.put("user_id", user_id);
        }
        if (lang != -1) {
            param.put("lang", lang);
        }
        if (result != -1) {
            param.put("result", result);
        }
        return iSolutionDao.selectByParam(param);
    }

    public int getCount(Integer contest_id, Integer pid, String user_id, Integer lang, Integer result) {
        Map<String, Object> param = new HashMap<>();
        if (contest_id != -1) {
            param.put("contest_id", contest_id);
        }
        if (pid != -1) {
            param.put("pid", pid);
        }
        if (!user_id.isEmpty()) {
            param.put("user_id", user_id);
        }
        if (lang != -1) {
            param.put("lang", lang);
        }
        if (result != -1) {
            param.put("result", result);
        }
        return iSolutionDao.selectCountByParam(param);
    }

    public int isAccepted(int problem_id, String user_id, int contest_id) {
        Map<String, Object> param = new HashMap<>();
        param.put("problem_id", problem_id);
        param.put("user_id", user_id);
        if (contest_id > 0) {
            param.put("contest_id", contest_id);
        }
        int a = iSolutionDao.selectCountByParam(param);
        if (a == 0) {
            return 0;
        }
        param.put("result", 4);
        a = iSolutionDao.selectCountByParam(param);
        return a == 0 ? 2 : 1;
    }

    public int isAccepted(int problem_id, String user_id) {
        return isAccepted(problem_id, user_id, -1);
    }

    public int submit(Solution solution, String code) {
        iSolutionDao.insert(solution);

        SourceCode sourceCode = new SourceCode();
        sourceCode.setSolution_id(solution.getSolution_id());
        sourceCode.setSource(code);
        iSourceCodeDao.insert(sourceCode);

        SourceCodeUser sourceCodeUser = new SourceCodeUser();
        sourceCodeUser.setSolution_id(solution.getSolution_id());
        sourceCodeUser.setSource(code);
        iSourceCodeUserDao.insert(sourceCodeUser);

        return solution.getSolution_id();
    }
}
